package adapter;

import java.util.List;

import com.cqtczh.qhd.driver.R;

import android.R.bool;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import javabeen.CarTeamIdBeen;
/** 
 * @author  作者 E-mail: 
 * @date 创建时间：2015-6-5 下午3:51:57 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */
public class CarTeamMemberListAdapter extends BaseAdapter {

	public List<CarTeamIdBeen> CarTeamIdBeen;
	private int xml;
	private Context context;
	private LayoutInflater inflater;
	/**
	 * 是否是公司订单
	 */
	private boolean isComplete=true;
	/**
	 * 
	 * @param context
	 * @param CarTeamIdBeen
	 * @param xml
	 * @param tyep 1、自由车队 2、公司车队
	 */
	public CarTeamMemberListAdapter(Context context, List<CarTeamIdBeen> CarTeamIdBeen,
			int xml,int tyep) {
		this.CarTeamIdBeen = CarTeamIdBeen;
		this.xml = xml;
		this.context = context;
		if (this.context != null) {
			this.inflater = LayoutInflater.from(context);
		}

	}
	
	
	public void setCarTeamType(boolean isComplete){
		this.isComplete=isComplete;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return CarTeamIdBeen.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return CarTeamIdBeen.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		MyListener myListener=null;
		if (convertView == null) {
			convertView = inflater.inflate(xml, null);
			viewHolder = new ViewHolder();
			myListener=new MyListener(position);
			viewHolder.Car_team_member_name = (TextView) convertView
					.findViewById(R.id.Car_team_member_name);
			viewHolder.Car_team_member_Mobile = (TextView) convertView
					.findViewById(R.id.Car_team_member_Mobile);
			viewHolder.Car_team_member_Address = (TextView) convertView
					.findViewById(R.id.Car_team_member_Address);
			viewHolder.Car_team_member_orders = (TextView) convertView
					.findViewById(R.id.Car_team_member_orders);
			viewHolder.Car_team_member_makeMoney = (TextView) convertView
					.findViewById(R.id.Car_team_member_makeMoney);
			viewHolder.Car_team_member_Flag = (TextView) convertView
					.findViewById(R.id.Car_team_member_Flag);
			viewHolder.callPhone=(Button) convertView.findViewById(R.id.call_driver);
			viewHolder.complete_team_info=convertView.findViewById(R.id.complete_team_info);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		CarTeamIdBeen data = CarTeamIdBeen.get(position);
		if (data != null) {
			if ((data.getCarownername()!=null)) {
				viewHolder.Car_team_member_name.setText(data.getCarownername());
			}else {
				viewHolder.Car_team_member_name.setText("司机未填写姓名");
			}
			// (R.id.myorder_content_flowid);
			viewHolder.Car_team_member_Mobile.setText(data.getPhone());
			final String tellString=data.getPhone();
			viewHolder.Car_team_member_Address.setText(data.getLocation());
			if (isComplete) {
				viewHolder.Car_team_member_orders.setText(data.getTotal()+"");
				viewHolder.Car_team_member_makeMoney.setText(data.getSummoney()+"");
			}else {
				viewHolder.complete_team_info.setVisibility(View.INVISIBLE);
			}
			
			if ((data.getOnline()!=null)&&(data.getOnline().equals("1"))) {
				viewHolder.Car_team_member_Flag.setText("在线");
			}
			else {
				viewHolder.Car_team_member_Flag.setText("已下线");
			}
			viewHolder.callPhone.setOnClickListener(new MyListener(position));
		//	viewHolder.callPhone.setText("直接联系他");
//			viewHolder.callPhone.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View arg0) {
//					// TODO Auto-generated method stub
//					Intent intent = new Intent();
//					// System.out.println("real电话");
//					// 激活源代码,添加intent对象
//					intent.setAction("android.intent.action.CALL");
//					// intent.addCategory("android.intent.category.DEFAULT");内部会自动添加类别，
//					intent.setData(Uri.parse("tel:" + tellString));
//					// 激活Intent
//					context.startActivity(intent);
//				}
//			});
		}

		return convertView;
	}
	
	
	class ViewHolder {
		TextView Car_team_member_name;
		TextView Car_team_member_Mobile;
		TextView Car_team_member_Address;
		TextView Car_team_member_orders;
		TextView Car_team_member_makeMoney;
		TextView Car_team_member_Flag;
		Button callPhone;
		View complete_team_info;
	}

	private class MyListener implements OnClickListener{
        int mPosition;
        public MyListener(int inPosition){
            mPosition= inPosition;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        	Log.v("点击了", "---->"+mPosition);
        	Intent intent = new Intent();
        	intent.setAction("android.intent.action.CALL");
        	intent.setData(Uri.parse("tel:" + CarTeamIdBeen.get(mPosition).getPhone()));
        	context.startActivity(intent);
        }
        
    }
	
	
	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		if (observer != null) {
			super.unregisterDataSetObserver(observer);
		}

	}


}
